package com.example.myapplication.Config;

import android.text.TextUtils;
import android.util.Log;

import com.google.gson.Gson;

import okhttp3.MediaType;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;

public class OkhttpUitls {
    private static String TOKEM ;
    private static String GET = "get";
    private static String POST = "post";
    private static String PUT = "put";
    private static String TAG = "TAG" ;
    private static  String URL  = "http://124.93.196.45:10001/" ;
    private static OkHttpClient okHttpClient;
    private static Gson gson;

    public static void init(){
        okHttpClient = null ;
        gson = null ;
        okHttpClient = new OkHttpClient();
        gson = new Gson();
    }

    public static void donetRequest(String url,String mothod,Class classofT,NetCallback netCallback,String... json){
        Request.Builder builder = new Request.Builder();
        if(!TextUtils.isEmpty(url)){
            url = URL +url;
            Log.e(TAG,"url "+url);
            builder.url(url);
        }

        if(!TextUtils.isEmpty(TOKEM)){
            Log.e(TAG,"TOKEM "+TOKEM);
            builder.addHeader("Authorization",TOKEM);
        }

        if(GET.equals(mothod)){
            builder.get();
        }else  if(POST.equals(mothod) && json.length>0){
            builder.post(RequestBody.create(json[0], MediaType.parse("application/json;charset=utf-8")));
        }else  if(PUT.equals(mothod) && json.length>0){
            builder.put(RequestBody.create(json[0], MediaType.parse("application/json;charset=utf-8")));
        }

        if(okHttpClient ==null || gson==null){
            init();
        }


        Request build = builder.build();


    }

    interface NetCallback{
        void onsucces(Object obj);
        void onfailure(String message);
    }
}
